package logic

import (
	"context"
	"errors"
	"strings"
	"video-hub/models/mysql"

	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 评论发布与校验
func (l *CommentAddLogic) CommentAdd(in *videorpc.CommentAddRequest) (*videorpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User
	err := findUser.FirstUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("请先登录")
	}
	var findVideo mysql.Video
	err = findVideo.FirstVideoById(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("该视频不存在")
	}
	if in.CommentContent == "" {
		return nil, errors.New("请输入评论内容")
	}
	if strings.Contains(in.CommentContent, "垃圾") == true || strings.Contains(in.CommentContent, "骗人") == true {
		return nil, errors.New("包含违规词汇，请修改后提交")
	}
	comment := mysql.Comment{
		VideoId:        in.VideoId,
		UserId:         in.UserId,
		CommentContent: in.CommentContent,
		IsAnonymous:    in.IsAnonymous,
	}
	err = comment.CommentAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("评论失败")
	}
	return &videorpc.CommentAddResponse{
		Comment: int64(comment.ID),
	}, nil
}
